package org.example.controller;

import org.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class StudentController {

    @RequestMapping(value = "/t0",method = RequestMethod.POST)
    public ModelAndView Test0(){
        System.out.println("---------------------t0loaded--------------------");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("t0");
        mv.addObject("t0", "现在是t0,POST");
        return mv;
    }


    @RequestMapping(value = "/t1",method = RequestMethod.GET)
    public ModelAndView Test1() {
        ModelAndView mv = new ModelAndView();
        System.out.println("---------------------t1loaded--------------------");
        mv.setViewName("t1");
        mv.addObject("t1", "现在是t1,GET");
        return mv;
    }


    @RequestMapping("/t2")
    public String Test2(){
        System.out.println("---------------------t2loaded--------------------");
        return "t2";
    }


    @RequestMapping("/t3")
//    直接返回字符串
    @ResponseBody
    public String Test3(){
        System.out.println("---------------------t3loaded--------------------");
        return "t3";
    }


    @RequestMapping("/t4")
    public String Test4(int id, String name, int age, Model model){
        System.out.println("---------------------t4loaded--------------------");
        System.out.println(id+" "+name+" "+age);
        model.addAttribute("id", id);
        model.addAttribute("name", name);
        model.addAttribute("age", age);
        return "t4";
    }

//    对象
    @RequestMapping("/t5")
    public String Test5(User user, Model model){
        System.out.println("---------------------t5loaded--------------------");
        System.out.println(user);
        model.addAttribute("id", user.getId());
        model.addAttribute("name", user.getName());
        model.addAttribute("age", user.getAge());
        return "t4";
    }
//    对象数组
//    @RequestMapping("/t6")
//    public String Test6(List<User> user, Model model){
//        System.out.println("---------------------t6loaded--------------------");
//        System.out.println(user.get(0).getId());
//        model.addAttribute("id", user.get(0).getId());
//        model.addAttribute("name", user.get(0).getName());
//        model.addAttribute("age", user.get(0).getAge());
//        return "t4";
//        }


    @RequestMapping("/t7")
    @ResponseBody
    public User Test7(@RequestBody User user){
        System.out.println("---------------------t7loaded--------------------");
        System.out.println(user);
        return user;
    }

    @RequestMapping("/t8")
    @ResponseBody
    public User Test7(){
        System.out.println("---------------------t8loaded--------------------");
        User user = new User();
        user.setId(1);
        user.setName("szt");
        user.setAge(20);
        System.out.println(user);
        return user;
    }

}
